package com.gongpingjialoan.activity.car;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.fb.MyPushIntentService;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.receiver.ModifyMileBoardCast;
import com.gongpingjialoan.utility.StepRecord;

/**
 * Created by Administrator on 2015/12/8.
 */
public class MyLikeCarActivity extends BaseActivity implements ModifyMileBoardCast.IModifyMile {

    private MyLikeCarFragment mMyLikeCarFragment;

    private ModifyMileBoardCast modifyMileBoardCast;

    private String fromType;

    private String action;

    private TextView managerTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_like_car_layout);
        fromType = getIntent().getStringExtra("type");
        managerTextView = (TextView) findViewById(R.id.right_title);
        managerTextView.setText("管理");
        managerTextView.setVisibility(View.VISIBLE);
        managerTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyLikeCarActivity.this, LikeCarActivity.class));
            }
        });
        String  job_id = getIntent().getStringExtra("job_id");
        String  source = getIntent().getStringExtra("source");
        if (MyPushIntentService.MYCAR.equals(fromType)) {
            StepRecord.recordStep(this, "pushnotification", "{type:" + fromType  + ",source:"  + source +  ",job_id:"  + job_id + "}");
        }
        setTitle("我的爱车");
        registerBoardcast();
        mMyLikeCarFragment = new MyLikeCarFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.content , mMyLikeCarFragment).commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(modifyMileBoardCast!=null){
            unregisterReceiver(modifyMileBoardCast);
            modifyMileBoardCast = null;
        }
    }

    public void registerBoardcast() {
        modifyMileBoardCast = new ModifyMileBoardCast();
        modifyMileBoardCast.setiModifyMile(this);
        IntentFilter intentFilter = new IntentFilter(ModifyMileBoardCast.MODIFY_MILE_ACTION);
        registerReceiver(modifyMileBoardCast, intentFilter);
    }

    @Override
    public void modifySuccess(Intent intent) {
        action = intent.getStringExtra("action");
        if("action".equals(action)){
            mMyLikeCarFragment.modifymiles(intent.getStringExtra("mile"));
        }else if("refresh".equals(action)){
            mMyLikeCarFragment.refresh(false);
        }else if("manager".equals(action)){
            mMyLikeCarFragment.refresh(false);
        }
    }

    @Override
    public void initTitleBar() {
        View backV = findViewById(R.id.title_back);
        if (backV != null) {
            backV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    onBackPressed();
                }
            });
        }
    }
    @Override
    public void onBackPressed() {
        //表示从推送而来
        if (MyPushIntentService.MYCAR.equals(fromType)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            super.onBackPressed();
        }
    }
}
